package com.example.config;

import cn.hutool.core.util.StrUtil;
import com.example.filter.CorsFilter;
import com.example.filter.IsLoginFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

/**
 * 过滤器配置
 *
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2022/10/18
 * @time: 21:53
 */
@Configuration
public class FilterConfig {

    //过滤器方法，当需要多种过滤情况时，可复用

    /**
     * 跨域过滤器
     * @return
     */
    @Bean
    public FilterRegistrationBean corsFilter() {
        List<String> excludedUris = Arrays.asList(".html", ".js", ".css", "/access/", "/res/", "/swagger-resources", "/v2/api-docs", "/wsChat");
        //指定过滤器
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new CorsFilter());

        //添加过滤路径
        filterRegistrationBean.addUrlPatterns("/*"); //("/...","/...")

        //设置忽略的路径
        filterRegistrationBean.addInitParameter("excludedUris", String.join(",", excludedUris));

        //order的数值越小 则优先级越高
        filterRegistrationBean.setOrder(1);

        filterRegistrationBean.setName("corsFilter");

        return filterRegistrationBean;
    }

//    //加解密过滤器
//    @Bean
//    public FilterRegistrationBean DeCodeFilterRegistrationBean() {
//        //指定过滤器
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new DoCodecFilter());
//
//        //添加过滤路径
//        filterRegistrationBean.addUrlPatterns("/*");
//
//        //设置忽略的路径
//        filterRegistrationBean.addInitParameter("excludedUris", "/access/,/res/,.html,.js,.css");
//
//        //order的数值越小 则优先级越高
//        filterRegistrationBean.setOrder(1);
//
//        filterRegistrationBean.setName("decodeFilter");
//
//        return filterRegistrationBean;
//    }

    //登录检验过滤器
    @Bean
    public FilterRegistrationBean isLoginFilterRegistrationBean() {
        List<String> excludedUris = Arrays.asList(".html", ".js", ".css", "/access/", "/res/", "/swagger-resources", "/v2/api-docs", "/wsChat");
        //指定过滤器
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new IsLoginFilter());

        //添加过滤路径
        filterRegistrationBean.addUrlPatterns("/*"); //("/...","/...")

        //设置忽略的路径
        filterRegistrationBean.addInitParameter("excludedUris", String.join(",", excludedUris));

        //order的数值越小 则优先级越高
        filterRegistrationBean.setOrder(2);

        filterRegistrationBean.setName("isLoginFilter");

        return filterRegistrationBean;
    }

    //权限检验过滤器
//    @Bean
//    public FilterRegistrationBean authorityFilterRegistrationBean(){
//        //指定过滤器
//        FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(new AuthorityFilter());
//
//        //添加过滤路径
//        filterRegistrationBean.addUrlPatterns("/access/,/res/,/webjars/,.html,.js,.css");
//
//        //设置忽略的路径
//        filterRegistrationBean.addInitParameter("excludedUris", "");
//
//        //order的数值越小 则优先级越高
//        filterRegistrationBean.setOrder(3);
//
//        filterRegistrationBean.setName("authorityFilter");
//
//        return filterRegistrationBean;
//    }

}
